package com.sun.mapstruct.demo;

import com.sun.mapstruct.beans.dto.CarDTO;
import com.sun.mapstruct.beans.dto.DriverDTO;
import com.sun.mapstruct.beans.dto.PartDTO;
import com.sun.mapstruct.beans.vo.CarVO;
import com.sun.mapstruct.beans.vo.DriverVO;
import com.sun.mapstruct.untils.ZhuanHuan;
import org.junit.Test;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Test01 {

    @Test
    public void demo(){
        CarDTO carDTO = createCarDto();
        System.out.println(carDTO);
        CarVO carVO = new CarVO();
        carVO.setId(carDTO.getId());
        carVO.setVin(carDTO.getVin());
        carVO.setPrice(carDTO.getPrice());
        carVO.setTotalPrice(new DecimalFormat("#.00").format(carDTO.getTotalPrice()));
        carVO.setPublishDate(new SimpleDateFormat("yyyy-MM-dd").format(carDTO.getPublishDate()));
        carVO.setBrandName(carDTO.getBrand());
        carVO.setHasPart(carDTO.getPartDTOS().get(0) == null ? true : false);
        DriverVO driverVO = new DriverVO();
        driverVO.setDriverId(carDTO.getDriverDTO().getId());
        driverVO.setFullName(carDTO.getDriverDTO().getName());
        carVO.setDriverVO(driverVO);
        System.out.println(carVO);
    }
    @Test
    public void demo2(){
        CarDTO carDTO = createCarDto();
        ZhuanHuan zhuanHuan = ZhuanHuan.INSTANCE;
        CarVO carVO = zhuanHuan.carDtoToCarVO(carDTO);
        System.out.println(carVO);
    }
    private CarDTO createCarDto() {
        CarDTO carDTO = new CarDTO();
        carDTO.setId(330L);
        carDTO.setVin("vin1234561");
        carDTO.setPrice(1239654879.123d);
        carDTO.setTotalPrice(1400000.321d);
        carDTO.setPublishDate(new Date());
        carDTO.setBrand("奔驰");
        carDTO.setColor("红色");
        PartDTO partDTO1 = new PartDTO();
        partDTO1.setPartId(1L);
        partDTO1.setPartName("全方位导航");
        PartDTO partDTO2 = new PartDTO();
        partDTO2.setPartId(2L);
        partDTO2.setPartName("智能车门");
        List<PartDTO> partDTOS = new ArrayList<>();
        partDTOS.add(partDTO1);
        partDTOS.add(partDTO2);
        carDTO.setPartDTOS(partDTOS);
        partDTO2.setPartName("多功能导弹");
        DriverDTO driverDTO = new DriverDTO();
        driverDTO.setId(1l);
        driverDTO.setName("小明");
        carDTO.setDriverDTO(driverDTO);
        driverDTO.setName("小王");
        return carDTO;
    }
}
